<template>
  <div class="user-menu">
    <div v-if="userStore.isLoggedIn" class="logged-in">
      <el-dropdown>
        <span class="el-dropdown-link">
          <div class="user-info">
            <span class="username">{{ userStore.userInfo?.username }}</span>
            <span class="email">{{ userStore.userInfo?.email }}</span>
          </div>
          <el-icon><arrow-down /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>
              <router-link to="/profile" class="dropdown-link">个人中心</router-link>
            </el-dropdown-item>
            <el-dropdown-item>
              <router-link to="/favorites" class="dropdown-link">我的收藏</router-link>
            </el-dropdown-item>
            <el-dropdown-item>
              <router-link to="/history" class="dropdown-link">观看记录</router-link>
            </el-dropdown-item>
            <el-dropdown-item divided @click="userStore.logout">
              <span class="logout-text">退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <div v-else class="logged-out">
      <el-button type="primary" link>
        <router-link to="/login" class="login-link">登录/注册</router-link>
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { useUserStore } from '../store/user';
import { ArrowDown } from '@element-plus/icons-vue';

const userStore = useUserStore();
</script>

<style scoped>
.user-menu {
  position: relative;
}

.logged-in {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.el-dropdown-link:hover {
  background: rgba(255, 255, 255, 0.1);
}

.user-info {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.username {
  font-weight: 600;
  color: var(--text);
}

.email {
  font-size: 12px;
  color: var(--muted);
}

.dropdown-link {
  text-decoration: none;
  color: inherit;
  display: block;
  width: 100%;
  padding: 4px 0;
}

.logout-text {
  color: #e53e3e;
}

.logged-out {
  display: flex;
  align-items: center;
}

.login-link {
  text-decoration: none;
  color: inherit;
}

@media (max-width: 768px) {
  .el-dropdown-link {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .user-info {
    align-items: flex-start;
  }
}
</style>